package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import com.hq.ims.data.dao.CompanyAptitudeMapper;
import com.hq.ims.data.entity.CompanyAptitude;
import com.hq.ims.data.entity.FileRecord;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.vo.AppFileMapstruct;
import com.hq.ims.data.vo.CompanyAptitudeMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.JsonUtils;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.ObjectUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 蒋柏平
 * @detail 企业资质 服务实现类
 * @since 20241022
 */
@Service
public class BaseCompanyAptitudeService extends BaseService<CompanyAptitudeMapper, CompanyAptitude> {

    @Resource
    private BaseFileRecordService baseFileRecordService;

    /**
     * 分页
     */
    public List<CompanyAptitudeMapstruct.CompanyAptitudeVo> getByPage(
            RequestPage page,
            CompanyAptitudeMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<CompanyAptitude> lq = this.lambdaQuery();
        //查询条件

        //执行分页
        lq.page(page);

        List<CompanyAptitude> dataList = page.getRecords();
        List<CompanyAptitudeMapstruct.CompanyAptitudeVo> voList = CompanyAptitudeMapstruct.INSTANCE.toCompanyAptitudeVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public CompanyAptitude addData(CompanyAptitudeMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);

        //账值新的对象
        CompanyAptitude entity = new CompanyAptitude();
        entity.setCompanyId(vo.getCompanyId());
        entity.setCompanyAptitudeName(vo.getCompanyAptitudeName());
        entity.setLv(vo.getLv());
        entity.setValidEndDate(vo.getValidEndDate());

        entity.insert();

        //保存资质文件
        AppFileMapstruct.FileVo fileVo = vo.getFileVo();
        if (fileVo != null) {
            baseFileRecordService.saveFile(fileVo, CompanyAptitude.class, entity.getCompanyAptitudeId(), LocalConstant.FILE_COMPANY_DIVIDE_INTO_RULE, true);
        }
        return entity;
    }

    /**
     * 编辑
     */
    @Transactional(rollbackFor = Exception.class)
    public CompanyAptitude editData(CompanyAptitudeMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdEdit.class);

        //账值新的对象
        CompanyAptitude entity = this.getById(vo.getCompanyAptitudeId());
        ObjectUtils.isNullThrow(entity, "数据不存在");

        entity.setCompanyId(vo.getCompanyId());
        entity.setLv(vo.getLv());
        entity.setValidEndDate(vo.getValidEndDate());

        entity.updateById();
        return entity;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String companyAptitudeId) {
        CompanyAptitude data = this.getById(companyAptitudeId);
        if (data == null) {
            return;
        }
        data.deleteById();
    }

    /**
     * 获取原始数据详情
     */
    public CompanyAptitudeMapstruct.CompanyAptitudeVo getDetailVo(String companyAptitudeId) {
        CompanyAptitude entity = this.getById(companyAptitudeId);
        CompanyAptitudeMapstruct.CompanyAptitudeVo vo = CompanyAptitudeMapstruct.INSTANCE.toCompanyAptitudeVo(entity);
        return vo;
    }

    public List<CompanyAptitudeMapstruct.CompanyAptitudeVo> getAptitudeInfoByCompanyId(String companyId) {
        List<CompanyAptitude> aptitudes = this.lambdaQuery().eq(CompanyAptitude::getCompanyId, companyId).list();
        List<CompanyAptitudeMapstruct.CompanyAptitudeVo> aptitudeVos = JsonUtils.convertListObject(aptitudes, CompanyAptitudeMapstruct.CompanyAptitudeVo.class);
        for (CompanyAptitudeMapstruct.CompanyAptitudeVo aptitudeVo : aptitudeVos) {
            List<FileRecord> file = baseFileRecordService.getFile(aptitudeVo.getCompanyAptitudeId(), LocalConstant.FILE_COMPANY_APTITUDE);
            List<AppFileMapstruct.FileVo> fileVos = baseFileRecordService.changeFileRecordToFileVo(file);
            aptitudeVo.setFiles(fileVos);
        }
        return aptitudeVos;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByCompanyId(String companyId) {
        List<CompanyAptitude> dataList = this.lambdaQuery().select(CompanyAptitude::getCompanyAptitudeId).eq(CompanyAptitude::getCompanyId, companyId).list();
        if (ListUtils.isExistsEle(dataList)) {
            List<String> dataIdList = dataList.stream().map(CompanyAptitude::getCompanyAptitudeId).distinct().collect(Collectors.toList());
            for (String dataId : dataIdList) {
                baseFileRecordService.delFile(CompanyAptitude.class, dataId);
            }
            this.lambdaUpdate().eq(CompanyAptitude::getCompanyId, companyId).remove();
        }
    }
}